import pyperclip
from typing import List


class LineDecorator:
    """按每组行处理指定行，加前后缀字符串"""

    def __init__(self, n: int, m: int, prefix: str = '', suffix: str = ''):
        """
        n: 每组行数
        m: 要修改的行号（从1开始）
        prefix: 添加在行首的字符串
        suffix: 添加在行尾的字符串
        """
        self.n = n
        self.m = m
        self.prefix = prefix
        self.suffix = suffix

    @staticmethod
    def read_input() -> List[str]:
        """从控制台读取多行文本，连续空行结束"""
        print("请输入多行文本，输入空行结束（按两次 Enter 结束）：")
        lines = []
        empty_count = 0
        while True:
            line = input()
            if line == "":
                empty_count += 1
                if empty_count >= 2:
                    break
            else:
                empty_count = 0
            lines.append(line)
        # 去掉尾部连续空行
        while lines and lines[-1] == "":
            lines.pop()
        return lines

    def process_lines(self, lines: List[str]) -> List[str]:
        """处理多行文本"""
        for idx in range(len(lines)):
            if idx % self.n == self.m - 1:
                lines[idx] = f"{self.prefix}{lines[idx]}{self.suffix}"
        return lines

    def process_and_copy(self, lines: List[str]) -> str:
        """处理文本并复制到剪贴板"""
        processed_lines = self.process_lines(lines)
        result = "\n".join(processed_lines)
        pyperclip.copy(result)
        return result


if __name__ == "__main__":
    n = int(input("请输入每组的行数 n："))
    m = int(input("请输入要修改的第 m 行："))
    prefix = input("请输入要添加在开头的字符串 x：")
    suffix = input("请输入要添加在末尾的字符串 y：")

    lines = LineDecorator.read_input()
    decorator = LineDecorator(n=n, m=m, prefix=prefix, suffix=suffix)
    final_text = decorator.process_and_copy(lines)

    print("\n处理后的文本：\n")
    print(final_text)
    print("\n✅ 已复制到剪贴板！")
